// file1.js
// const redis = require('redis')
// const client = redis.createClient()

// client.on('error',error=>{
//     console.log('Error',error)
// })

// console.log('Setting key1')
// client.set('key1', 'My string', redis.print)
// console.log('Getting key1')
// client.get('key1', (err,reply)=>{
//     console.log('results for key1')
//     console.log(reply)
//     client.end()
// })

// file2.js
// const redis = require('redis')
// const client = redis.createClient('6379', '127.0.0.1', {
//     password: 'asdasd'
// })

// client.on('error',err=>{
//     console.log('Error', err)
// })

// console.log('Setting user hash')

// client.hset('user', 'username', 'johndoe')
// client.hset('user', 'firstname', 'john')
// client.hset('user', 'lastname', 'doe')
// client.hkeys('user', (err,replies)=>{
//     console.log('Results for user')
//     console.log(replies.length + ' replies:')
//     replies.forEach((reply, i)=>{
//         console.log(i,": ", reply)
//     })
//     client.end()
// })

// client.set('user1', 'xiaoming')

// client.del('user1', (err, res)=>{
//     console.log('user1 deleted', res)
// })

// client.exists('user1', (error, result)=>{
//     console.log(error, result, 'result')
// })
// client.get('user1',(err1, res1)=>{
//     if (err1) {
//         console.log('get user1 error', err1)
//         throw new Error(err1)
//     } else {
//         console.log('\'string\' user1:', res1)
//     }
// })

/**
 *  file3.js 发布订阅功能
 *  */
// const redis = require('redis')
// const client1 = redis.createClient(6379, '127.0.0.1')
// const client2 = redis.createClient(6379, '127.0.0.1')
// const pub = redis.createClient(6379, '127.0.0.1')

// const clients = []

// clients.push(client1)
// clients.push(client2)

// const channelName = 'emergency channel'

// client1.on('message',(channel, count)=>{
//     console.log(channel, count)
// })

// client2.on('message',(channel, count)=>{
//     console.log(channel, count)
// })

// let clientCount = 2
// while(clientCount > 0) {
//     const client = clients[clientCount - 1]
//     client.subscribe(channelName)
//     clientCount--
// }

// let i = 1
// setInterval(()=>{
//     pub.publish(channelName, `This are ${i} apple`)
//     i++
// }, 3000)

/**
 * npm redis 官方案例
 * file4.js
 *  */
// var redis = require("redis");
// var sub = redis.createClient(), pub = redis.createClient();
// var msg_count = 0;

// sub.on("subscribe", function (channel, count) {
//     pub.publish("a nice channel", "I am sending a message.");
//     pub.publish("a nice channel", "I am sending a second message.");
//     pub.publish("a nice channel", "I am sending my last message.");
// });
 
// sub.on("message", function (channel, message) {
//     console.log("event message: sub channel " + channel + ": " + message);
//     msg_count += 1;
//     if (msg_count === 3) {
//         sub.unsubscribe();
//         sub.quit();
//         pub.quit();
//     }
// });
 
// sub.subscribe("a nice channel");


/**
 * 事务 
 * multi
 * execAsync
 **/
const redis = require('redis')
const bluebird = require('bluebird')
bluebird.promisifyAll(redis.Multi.prototype)
const client = redis.createClient(6379, '127.0.0.1')

client.multi().set('username', 'xiaoming').get('username').del('username').execAsync().then(res => {
    console.log(res)
})

client.get('username', (err, res)=>{
    console.log('get username ', res)
})
